// 引入express模块
const express = require('express')
// 创建app对象
const app = express()
// 引入body-parser
var bodyParser = require('body-parser');
// multer模块,nodejs中间件,用于上传图片
var multer = require('multer')

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());

// 解决cors问题
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  // res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", ' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  res.header("Access-Control-Allow-Headers", "Content-Type,XFILENAME,XFILECATEGORY,XFILESIZE,X-Requested-With,Authorization");
  next();
});

// 利用express托管静态文件
app.use(express.static('public'))

// 引入文章操作api
const article = require('./router/article')
app.use('/api', article)

// 引入操作时间线api
const timeline = require('./router/timeline')
app.use('/api', timeline)

// 引入评论区api
const comment = require('./router/comment')
app.use('/api', comment)

// 引入站长回复api
const replycomment = require('./router/replycomment')
app.use('/api', replycomment)

// 引入登录api
const login = require('./router/login')
app.use('/api', login)

// 引入上传图片api
const upload = require('./router/upload')
app.use('/api', upload)

// 定义服务启动端口
app.listen(3000, () => {
  console.log('app listening on port 3000...')
})
